package com.bawei.image.glide

import android.content.Context
import com.bumptech.glide.GlideBuilder
import com.bumptech.glide.annotation.GlideModule
import com.bumptech.glide.load.engine.cache.InternalCacheDiskCacheFactory
import com.bumptech.glide.load.engine.cache.LruResourceCache
import com.bumptech.glide.load.engine.cache.MemorySizeCalculator
import com.bumptech.glide.module.AppGlideModule

/**
 * Author: Jason Zhang
 * Date: 2024/4/23
 * Desc: 使用GlideModule来配置Glide的内存和磁盘缓存大小
 */
@GlideModule
class GlideModule: AppGlideModule() {

    override fun applyOptions(context: Context, builder: GlideBuilder) {
        //获取内存计算器
        var memorySizeCalculator = MemorySizeCalculator.Builder(context).build()

        //计算APP的1/4的内存大小
        var glideMemorySize = memorySizeCalculator.memoryCacheSize / 8L

        //修改Glide的内存缓存大小
        builder.setMemoryCache(LruResourceCache(glideMemorySize))

        //修改Glide的磁盘缓存大小
        builder.setDiskCache(InternalCacheDiskCacheFactory(context, "imgCache", 200 * 1024 * 1024))

    }

}